#!/bin/bash
echo "===== 智能模块启动脚本 ====="

# 定位JAR文件
JAR_PATH=$(find /agents/target -name "smart-module-*.jar" | sort -r | head -1)
if [ -z "$JAR_PATH" ]; then
  echo "❌ 错误：未找到JAR文件"
  echo "搜索路径: /agents/target"
  find /agents/target
  exit 1
fi
echo "找到模块文件: $JAR_PATH"

# 启动模块
echo "启动智能模块..."
nohup java -jar "$JAR_PATH" > /agents/module.log 2>&1 &
START_PID=$!
echo "启动进程ID: $START_PID"

# 验证启动
sleep 10
if ps -p $START_PID > /dev/null; then
  echo "✅ 启动成功 | 运行PID: $START_PID"
  echo "===== 最近日志 ====="
  tail -n 15 /agents/module.log
else
  echo "❌ 启动失败！完整日志："
  cat /agents/module.log
  exit 1
fi

# 保持运行状态
echo "===== 进入保持运行模式 ====="
echo "此任务将持续运行直到超时（最长2小时）"
counter=0
while [ $counter -lt 120 ]; do
  sleep 60
  ((counter++))
  echo "[运行时长: ${counter}分钟] 智能模块状态正常 | 日志大小: $(du -sh /agents/module.log)"
done
echo "🕒 已达到最大运行时限制"